{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This allows multiple outputs from a single jupyter notebook cell:\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.0.3'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import pandas as pd\n",
    "pd.__version__  # for the record"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(252, 9)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Adj Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>UpperB</th>\n",
       "      <th>LowerB</th>\n",
       "      <th>PercentB</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2011-07-01</th>\n",
       "      <td>132.089996</td>\n",
       "      <td>134.100006</td>\n",
       "      <td>131.779999</td>\n",
       "      <td>133.919998</td>\n",
       "      <td>117.161659</td>\n",
       "      <td>202385700</td>\n",
       "      <td>132.373927</td>\n",
       "      <td>125.316073</td>\n",
       "      <td>1.219057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-07-05</th>\n",
       "      <td>133.779999</td>\n",
       "      <td>134.080002</td>\n",
       "      <td>133.389999</td>\n",
       "      <td>133.809998</td>\n",
       "      <td>117.065437</td>\n",
       "      <td>165936000</td>\n",
       "      <td>133.254297</td>\n",
       "      <td>124.912703</td>\n",
       "      <td>1.066618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-07-06</th>\n",
       "      <td>133.490005</td>\n",
       "      <td>134.139999</td>\n",
       "      <td>133.110001</td>\n",
       "      <td>133.970001</td>\n",
       "      <td>117.205429</td>\n",
       "      <td>143331600</td>\n",
       "      <td>134.040915</td>\n",
       "      <td>124.627085</td>\n",
       "      <td>0.992467</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Open        High         Low       Close   Adj Close  \\\n",
       "Date                                                                     \n",
       "2011-07-01  132.089996  134.100006  131.779999  133.919998  117.161659   \n",
       "2011-07-05  133.779999  134.080002  133.389999  133.809998  117.065437   \n",
       "2011-07-06  133.490005  134.139999  133.110001  133.970001  117.205429   \n",
       "\n",
       "               Volume      UpperB      LowerB  PercentB  \n",
       "Date                                                     \n",
       "2011-07-01  202385700  132.373927  125.316073  1.219057  \n",
       "2011-07-05  165936000  133.254297  124.912703  1.066618  \n",
       "2011-07-06  143331600  134.040915  124.627085  0.992467  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Adj Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>UpperB</th>\n",
       "      <th>LowerB</th>\n",
       "      <th>PercentB</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012-06-27</th>\n",
       "      <td>132.419998</td>\n",
       "      <td>133.429993</td>\n",
       "      <td>131.970001</td>\n",
       "      <td>133.169998</td>\n",
       "      <td>118.980804</td>\n",
       "      <td>108088000</td>\n",
       "      <td>136.447962</td>\n",
       "      <td>128.140042</td>\n",
       "      <td>0.605441</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-06-28</th>\n",
       "      <td>132.289993</td>\n",
       "      <td>132.990005</td>\n",
       "      <td>131.279999</td>\n",
       "      <td>132.789993</td>\n",
       "      <td>118.641281</td>\n",
       "      <td>169242100</td>\n",
       "      <td>136.500761</td>\n",
       "      <td>128.219241</td>\n",
       "      <td>0.551922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-06-29</th>\n",
       "      <td>135.199997</td>\n",
       "      <td>136.270004</td>\n",
       "      <td>134.850006</td>\n",
       "      <td>136.100006</td>\n",
       "      <td>121.598610</td>\n",
       "      <td>212250900</td>\n",
       "      <td>136.721010</td>\n",
       "      <td>128.792993</td>\n",
       "      <td>0.921670</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Open        High         Low       Close   Adj Close  \\\n",
       "Date                                                                     \n",
       "2012-06-27  132.419998  133.429993  131.970001  133.169998  118.980804   \n",
       "2012-06-28  132.289993  132.990005  131.279999  132.789993  118.641281   \n",
       "2012-06-29  135.199997  136.270004  134.850006  136.100006  121.598610   \n",
       "\n",
       "               Volume      UpperB      LowerB  PercentB  \n",
       "Date                                                     \n",
       "2012-06-27  108088000  136.447962  128.140042  0.605441  \n",
       "2012-06-28  169242100  136.500761  128.219241  0.551922  \n",
       "2012-06-29  212250900  136.721010  128.792993  0.921670  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('../data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)\n",
    "#df = df.loc['2012-01-01':,:]\n",
    "df.shape\n",
    "df.head(3)\n",
    "df.tail(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib qt\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# def plotbars(v,aa=None,w=0.5,e=0.5):\n",
    "#     x = range(len(v))\n",
    "#     fig, ax = plt.subplots()\n",
    "#     ax.bar(x,v,antialiased=aa,width=w,linewidth=0.0)\n",
    "#     plt.show(block=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plotbars(v,fid=1,dpi=100,**kwargs):\n",
    "    x = range(len(v))\n",
    "    fig = plt.figure(fid,dpi=dpi)\n",
    "    ax  = fig.add_subplot()\n",
    "    ax.bar(x,v,**kwargs)\n",
    "    plt.show(block=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "len(v1)= 50\n",
      "len(v2)= 150\n"
     ]
    }
   ],
   "source": [
    "v1 = [202385700, 165936000, 143331600, 170464200, 194100500, 195918600, 214675700,\n",
    "      204062600, 226111800, 220012800, 196872100, 166554900, 137145400, 245246300,\n",
    "      126019400, 136653800, 131278200, 249020100, 207939900, 307038400, 325790900,\n",
    "      346653800, 370830800, 520721800, 655619200, 702263900, 717828700, 662607400,\n",
    "      487979700, 313731600, 258810600, 294095200, 238201100, 512956300, 428281300,\n",
    "      275090600, 331136600, 246869700, 312365400, 314495900, 190977200, 241315700,\n",
    "      301828400, 254585900, 255517200, 285130500, 209803200, 250568200, 380195100,\n",
    "      305793500]\n",
    "\n",
    "v2 = v1 + v1 + v1\n",
    "\n",
    "print('len(v1)=',len(v1))\n",
    "print('len(v2)=',len(v2))\n",
    "\n",
    "\n",
    "# plotbars(v1,w=0.2,e=0)\n",
    "# plotbars(v2,w=0.2,e=0)\n",
    "\n",
    "# plotbars(v1,w=.98,e=0)\n",
    "# plotbars(v2,w=.98,e=0)\n",
    "\n",
    "# plotbars(v1,w=1.0,e=0)\n",
    "# plotbars(v2,w=1.0,e=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-8-a6774c8535dd>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-8-a6774c8535dd>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    STOP HERE\u001b[0m\n\u001b[0m            ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "STOP HERE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plotbars(v1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plotbars(v2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def adjust_brightness(color, amount=0.5):\n",
    "    import matplotlib.colors as mc\n",
    "    import colorsys\n",
    "    try:\n",
    "        c = mc.cnames[color]\n",
    "    except:\n",
    "        c = color\n",
    "    c = colorsys.rgb_to_hls(*mc.to_rgb(c))\n",
    "    return colorsys.hls_to_rgb(c[0], max(0, min(1, amount * c[1])), c[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c1 = 'c'\n",
    "c2 = adjust_brightness(c1,0.75)\n",
    "print(c2)\n",
    "print(c1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plotbars(v2,1,dpi=135,ec=c2,fc=c1,linewidth=0.8,width=0.8)\n",
    "plotbars(v2,2,dpi=135,ec=c2,fc=c1,linewidth=0.5,width=0.5)\n",
    "#plotbars(v2,2,dpi=150,ec='k',linewidth=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v3 = [ (v,float('nan')) for v in v2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v3 = [v for tup in v3 for v in tup]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(v3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plotbars(v2,w=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "plotbars(v3,w=1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "STOP HERE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = df['Volume'].values\n",
    "#v = df['Close'].values\n",
    "def plot_vol(v,aa=None,w=0.5):\n",
    "    fig = plt.figure(figsize=(20,12))\n",
    "    ax = fig.add_axes( [0.15, 0.18, 0.70, 0.70] )\n",
    "    x = range(len(v))\n",
    "    ax.bar(x,v,antialiased=aa,width=w)\n",
    "    #ax.plot(x,v,antialiased=aa)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "y = v[0:51]\n",
    "plot_vol(v,w=0.7)\n",
    "plot_vol(y,w=0.7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def adjust_brightness2(color, amount=0.5):\n",
    "    \n",
    "    def ab(c1, amount=0.5):\n",
    "        import matplotlib.colors as mc\n",
    "        import colorsys\n",
    "        try:\n",
    "            c = mc.cnames[c1]\n",
    "        except:\n",
    "            c = c1\n",
    "        c = colorsys.rgb_to_hls(*mc.to_rgb(c))\n",
    "        return colorsys.hls_to_rgb(c[0], max(0, min(1, amount * c[1])), c[2])\n",
    "\n",
    "    if not isinstance(color,(list,tuple)):\n",
    "        color = [color,]\n",
    "        \n",
    "    cout = []*len(color)\n",
    "    cadj = {}\n",
    "    for c1 in color:\n",
    "        if c1 in cadj:\n",
    "            cout.append(cadj[c1])\n",
    "        else:\n",
    "            newc = ab(c1,amount)\n",
    "            cadj[c1] = newc\n",
    "            cout.append(cadj[c1])\n",
    "\n",
    "    if len(cout) == 1:\n",
    "        return cout[0]\n",
    "    else:\n",
    "        return cout\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cadj = adjust_brightness2('c',0.75)\n",
    "cadj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cadj = adjust_brightness2(['c','r','g','b'],0.75)\n",
    "cadj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "vcolors= ['#007a00', '#d50d18', '#007a00', '#007a00', '#d50d18', '#d50d18', '#d50d18', \n",
    "          '#007a00', '#d50d18', '#007a00', '#d50d18', '#007a00', '#d50d18', '#007a00', \n",
    "          '#007a00', '#d50d18', '#d50d18', '#d50d18', '#d50d18', '#d50d18', '#d50d18', \n",
    "          '#d50d18', '#007a00', '#d50d18', '#d50d18', '#d50d18', '#007a00', '#d50d18', \n",
    "          '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', '#d50d18', '#d50d18', \n",
    "          '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', '#007a00', '#007a00', \n",
    "          '#007a00', '#d50d18', '#d50d18', '#d50d18', '#007a00', '#d50d18', '#d50d18', \n",
    "          '#007a00', '#007a00', '#007a00', '#007a00', '#007a00', '#d50d18', '#d50d18', \n",
    "          '#d50d18', '#d50d18', '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', \n",
    "          '#d50d18', '#d50d18', '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', \n",
    "          '#007a00', '#007a00', '#d50d18', '#007a00', '#d50d18', '#007a00', '#d50d18', \n",
    "          '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', '#007a00', '#d50d18', \n",
    "          '#d50d18', '#d50d18', '#007a00', '#007a00', '#d50d18', '#007a00', '#007a00', \n",
    "          '#d50d18', '#007a00', '#007a00', '#d50d18', '#007a00', '#d50d18', '#d50d18', \n",
    "          '#d50d18', '#d50d18', '#d50d18', '#d50d18', '#d50d18', '#007a00', '#007a00', \n",
    "          '#007a00', '#d50d18', '#d50d18', '#007a00', '#007a00', '#007a00', '#d50d18', \n",
    "          '#007a00', '#d50d18', '#d50d18', '#d50d18', '#007a00', '#d50d18', '#d50d18', \n",
    "          '#007a00', '#007a00', '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', \n",
    "          '#d50d18', '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', '#007a00', \n",
    "          '#007a00', '#007a00', '#d50d18', '#007a00', '#007a00', '#007a00', '#007a00', \n",
    "          '#d50d18', '#d50d18', '#007a00', '#d50d18', '#d50d18', '#d50d18', '#d50d18', \n",
    "          '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', '#007a00', '#007a00', \n",
    "          '#d50d18', '#007a00', '#d50d18', '#d50d18', '#007a00', '#007a00', '#007a00', \n",
    "          '#d50d18', '#007a00', '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', \n",
    "          '#d50d18', '#d50d18', '#d50d18', '#007a00', '#007a00', '#007a00', '#007a00', \n",
    "          '#007a00', '#d50d18', '#007a00', '#d50d18', '#007a00', '#d50d18', '#d50d18', \n",
    "          '#d50d18', '#007a00', '#007a00', '#d50d18', '#d50d18', '#d50d18', '#007a00', \n",
    "          '#007a00', '#d50d18', '#d50d18', '#d50d18', '#d50d18', '#d50d18', '#007a00', \n",
    "          '#007a00', '#d50d18', '#d50d18', '#007a00', '#d50d18', '#d50d18', '#007a00', \n",
    "          '#d50d18', '#007a00', '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', \n",
    "          '#d50d18', '#d50d18', '#d50d18', '#007a00', '#d50d18', '#d50d18', '#007a00', \n",
    "          '#d50d18', '#d50d18', '#d50d18', '#d50d18', '#d50d18', '#d50d18', '#007a00', \n",
    "          '#007a00', '#007a00', '#007a00', '#d50d18', '#007a00', '#d50d18', '#d50d18', \n",
    "          '#d50d18', '#d50d18', '#007a00', '#007a00', '#007a00', '#007a00', '#d50d18', \n",
    "          '#007a00', '#d50d18', '#007a00', '#007a00', '#007a00', '#007a00', '#d50d18', \n",
    "          '#d50d18', '#007a00', '#d50d18', '#007a00', '#007a00', '#d50d18', '#007a00']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def without_cache(vcolors):\n",
    "    vca = []*len(vcolors)\n",
    "    for c in vcolors:\n",
    "        vca.append(adjust_brightness(c))\n",
    "    return vca\n",
    "\n",
    "def with_cache(vcolors):\n",
    "    return adjust_brightness2(vcolors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%timeit a = without_cache(vcolors)\n",
    "print(len(a))\n",
    "a[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%timeit a = with_cache(vcolors)\n",
    "print(len(a))\n",
    "a[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mplfinance._helpers import _adjust_color_brightness"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "252\n",
      "252\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[(0.0, 0.23921568627450981, 0.0),\n",
       " (0.4176470588235295, 0.025490196078431338, 0.04705882352941171),\n",
       " (0.0, 0.23921568627450981, 0.0),\n",
       " (0.0, 0.23921568627450981, 0.0),\n",
       " (0.4176470588235295, 0.025490196078431338, 0.04705882352941171)]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vadj = _adjust_color_brightness(vcolors)\n",
    "print(len(vcolors))\n",
    "print(len(vadj))\n",
    "vadj[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "252\n",
      "252\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[(0.0, 0.3588235294117647, 0.0),\n",
       " (0.6264705882352942, 0.038235294117647034, 0.07058823529411759),\n",
       " (0.0, 0.3588235294117647, 0.0),\n",
       " (0.0, 0.3588235294117647, 0.0),\n",
       " (0.6264705882352942, 0.038235294117647034, 0.07058823529411759)]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vadj = _adjust_color_brightness(vcolors,0.75)\n",
    "print(len(vcolors))\n",
    "print(len(vadj))\n",
    "vadj[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(vcolors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(vcolors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "STOP HERE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mplfinance as mpf\n",
    "\n",
    "mpf.plot(df,volume=True,style='charles')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "Let's say we want to plot the Lower Bollinger band along with the basic OHLCV plot.  \n",
    "\n",
    "We use `make_addplot()` to create the addplot dict, and pass that into the plot() function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "apdict = mpf.make_addplot(df['LowerB'])\n",
    "\n",
    "mpf.plot(df,volume=True,addplot=apdict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "When creating the `addplot` dict, we can specify that we want a scatter plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "apd = mpf.make_addplot(df['LowerB'],type='scatter')\n",
    "\n",
    "mpf.plot(df,addplot=apd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "The above example is a trivial use of a scatter plot, where the default line plot makes more sense.  \n",
    "\n",
    "A more helpful use of a scatter plot might be to highlight specific movements in the data.  For example, let's say we want to highlight whenever the \"Percent B\" Bollinger metric drops below zero.  To do this, let's first calculate a series that contains this information:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def percentB_belowzero(percentB,price):\n",
    "    import numpy as np\n",
    "    signal   = []\n",
    "    previous = -1.0\n",
    "    for date,value in percentB.iteritems():\n",
    "        if value < 0 and previous >= 0:\n",
    "            signal.append(price[date]*0.99)\n",
    "        else:\n",
    "            signal.append(np.nan)\n",
    "        previous = value\n",
    "    return signal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "Take a small data set, and calculate a series that shows when the percentB falls below zero:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tdf = df.loc['05-10-2012':'06-07-2012',]  # Take a smaller data set so it's easier to see the scatter points\n",
    "\n",
    "signal = percentB_belowzero(tdf['PercentB'], tdf['Close'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "Now plot the calculated information as an additional scatter plot on top of the the OHLC data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "apd = mpf.make_addplot(signal,type='scatter')\n",
    "\n",
    "mpf.plot(tdf,addplot=apd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "We can customize the marker size and shape, to make the scatter markers easier to see:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "apd = mpf.make_addplot(signal,type='scatter',markersize=200,marker='^')\n",
    "\n",
    "mpf.plot(tdf,addplot=apd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Plotting multiple additional data sets\n",
    "\n",
    "There are two ways to plot multiple additional data sets.\n",
    "\n",
    "- If the configuration is the same for all additional data sets, simply pass a `DataFrame` for the data.  All columns in the DataFrame will be plotted.\n",
    "\n",
    "- Alternatively you can create multiple `dict`s and pass a `list` of `dict`s to the `addplot` keyword\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "Passing a DataFrame as the addplot data plots all columns in the DataFrame:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tcdf = df[['LowerB','UpperB']]  # DataFrame with two columns\n",
    "apd  = mpf.make_addplot(tcdf)\n",
    "mpf.plot(df,addplot=apd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "Setting `addplot=` a `list` of `dict`s is another to create multiple additional plots.<br>This method is necessary if the additional plots will have different configurations.  For example:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "First prepare the data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def percentB_aboveone(percentB,price):\n",
    "    import numpy as np\n",
    "    signal   = []\n",
    "    previous = 2\n",
    "    for date,value in percentB.iteritems():\n",
    "        if value > 1 and previous <= 1:\n",
    "            signal.append(price[date]*1.01)\n",
    "        else:\n",
    "            signal.append(np.nan)\n",
    "        previous = value\n",
    "    return signal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "low_signal  = percentB_belowzero(df['PercentB'], df['Close']) \n",
    "high_signal = percentB_aboveone(df['PercentB'], df['Close'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "Now create the additional plot `dict`s and plot the data: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "apds = [ mpf.make_addplot(tcdf),\n",
    "         mpf.make_addplot(low_signal,type='scatter',markersize=200,marker='^'),\n",
    "         mpf.make_addplot(high_signal,type='scatter',markersize=200,marker='v'),\n",
    "       ]\n",
    "\n",
    "mpf.plot(df,addplot=apds,figscale=1.25,volume=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Plotting additional data on panel \"B\"\n",
    "\n",
    "---\n",
    "We refer to the Main, Upper panel as Panel \"A\" and the Lower panel as Panel \"B\".\n",
    "\n",
    "It is possible to plot the additional data on Panel \"B\" (where volume is usually plotted).\n",
    "In this example, as is typical in Bollinger Band Analysis, we plot `PercentB` in panel B:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "apds = [ mpf.make_addplot(tcdf),\n",
    "         mpf.make_addplot(low_signal,type='scatter',markersize=200,marker='^'),\n",
    "         mpf.make_addplot(high_signal,type='scatter',markersize=200,marker='v'),\n",
    "         mpf.make_addplot((df['PercentB']),panel=1,color='g')\n",
    "       ]\n",
    "\n",
    "mpf.plot(df,addplot=apds,figscale=1.3,volume=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Plotting additional data with a *secondary y-axis*\n",
    "\n",
    "---\n",
    "\n",
    "- Notice in the above plot, in the lower panel with the \"Volume\" bars, we see that \"Percent B\" line ***has its own y-axis on the right side.***\n",
    "\n",
    "\n",
    "- `mpf.make_addplot()` has a keyword argument called `secondary_y` which can have **three** possible values: **`True`**, **`False`**, and **`'auto'`**.\n",
    "  - The default value is `'auto'` which means if you don't specify `secondary_y`, or if you specify `secondary_y='auto'`, then `mpf.plot()` will attempt to decide whether a secondary y-axis is needed, by comparing the order of magnitude of the addplot data with the order of magnitude of the data that is already on the plot.\n",
    "  - If **`mpf.plot()`** gets it wrong, you can always override by setting **`secondary_y=True`** or **`secondary_y=False`**.\n",
    "  \n",
    "---\n",
    "\n",
    "- Below we see that `make_addplot()` also allows setting the `linestyle` for each additional plot.\n",
    "\n",
    "\n",
    "- **Notice also** that we pass an alternative \"mplfinance `style`\" to demonstrate that **if the `style` specifies that the *primary y-axis* should be on the right, then `mpf.plot()` knows to put any `secondary_y` axes on the left.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "apds = [ mpf.make_addplot(tcdf,linestyle='dashdot'),\n",
    "         mpf.make_addplot(low_signal,type='scatter',markersize=200,marker='^'),\n",
    "         mpf.make_addplot(high_signal,type='scatter',markersize=200,marker='v'),\n",
    "         mpf.make_addplot((df['PercentB']),panel=1,color='g',linestyle='dotted')\n",
    "       ]\n",
    "\n",
    "mpf.plot(df,addplot=apds,figscale=1.5,volume=True,style='starsandstripes')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "---\n",
    "\n",
    "* **Below** we demonstrate that the main (upper) panel can also have a `secondary_y` axis:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "# Change order of magnitude and range of low_signal, so that it will require a `secondary_y`:\n",
    "# note: this calculation has no financial meaning whatsoever; we are just generating some \n",
    "#       data to modify the order of magnitude and range, so as to be able to demonstrate \n",
    "#       secondary_y on the main panel.\n",
    "new_low_signal = [x*20.*math.sin(x) for x in low_signal] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "apds = [mpf.make_addplot(tcdf,linestyle='dashdot'),\n",
    "        mpf.make_addplot(new_low_signal,type='scatter',markersize=200,marker='^',secondary_y='auto'),\n",
    "        mpf.make_addplot(high_signal,type='scatter',markersize=200,marker='v',color='orange'),\n",
    "        mpf.make_addplot((df['PercentB']),panel=1,color='g',linestyle='dotted')\n",
    "       ]\n",
    "\n",
    "mpf.plot(df,addplot=apds,figscale=1.5,volume=True,style='sas')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The same plot, with a style that puts the primary y-axis on the left:\n",
    "mpf.plot(df,addplot=apds,figscale=1.5,volume=True,style='default')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
